-- Rippof Ammo's excellent MasterLoot
local function CandidateUnitClass(candidate)
	local class, fileName = UnitClass(candidate)
	if class then
		return fileName, class
	end
	return "UNKNOWN", "Classe Inconnue"
end

local function Sort_Candidates(a, b)
	return a[1] < b[1]
end

local function GroupLootDropDown_GiveLoot(self)
	if LootFrame.selectedQuality >= MASTER_LOOT_THREHOLD then
		local dialog = StaticPopup_Show("CONFIRM_LOOT_DISTRIBUTION",
			ITEM_QUALITY_COLORS[LootFrame.selectedQuality].hex..LootFrame.selectedItemName..FONT_COLOR_CODE_CLOSE,
			GetMasterLootCandidate(self.value))
		if dialog then
			dialog.data = self.value
		end
	else
		GiveMasterLoot(LootFrame.selectedSlot, self.value)
	end
	CloseDropDownMenus()
end

local menuColorCodes = {}
for name, color in pairs(RAID_CLASS_COLORS) do
	menuColorCodes[name] =  string.format("|cff%2x%2x%2x", color.r * 255, color.g * 255, color.b * 255)
end
menuColorCodes["UNKNOWN"] =  GRAY_FONT_COLOR_CODE

local function GroupLootDropDown_InitializeByClass(self, class, level, menuList)
	local candidates = {}
	for i = 1, 40 do
		local candidate = GetMasterLootCandidate(i)
		if candidate and CandidateUnitClass(candidate) == class then
			candidates[#candidates + 1] = {candidate, i}
		end
	end
	if #candidates == 0 then return end

	table.sort(candidates, Sort_Candidates)

	local info = UIDropDownMenu_CreateInfo()
	info.textHeight = 12
	info.notCheckable = 1
	info.func = GroupLootDropDown_GiveLoot
	info.colorCode = menuColorCodes[class]
	for _, entry in ipairs(candidates) do
		info.text = entry[1]
		info.value = entry[2]
		UIDropDownMenu_AddButton(info, level)
	end
end

local function GroupLootDropDown_Initialize(self, level, menuList)
	if level == 2 then
		return GroupLootDropDown_InitializeByClass(self, UIDROPDOWNMENU_MENU_VALUE, level, menuList)
	end

	local info = UIDropDownMenu_CreateInfo()

	info.textHeight = 12
	info.notCheckable = 1

	info.text = GIVE_LOOT
	info.isTitle = 1
	UIDropDownMenu_AddButton(info, level)
	info.isTitle = nil
	info.disabled = nil

	local randoms = {}
	local playerName = UnitName"player"
	local playerIndex
	if GetNumRaidMembers() > 0 then
		-- In a raid
		local classesInRaid = {}
		local BC = LibStub("Babble-Class-3.0", true)
		BC = BC and BC:GetUnstrictLookupTable()
		for i = 1, 40 do
			local candidate = GetMasterLootCandidate(i)
			if candidate then
				randoms[#randoms + 1] = i
				if candidate == playerName then playerIndex = i end
				local class, cname = CandidateUnitClass(candidate)
				classesInRaid[class] = BC and BC[class] or cname
			end
		end

		info.hasArrow = 1
		for class, name in pairs(classesInRaid) do
			info.text = name
			info.value = class
			info.colorCode = menuColorCodes[class]
			UIDropDownMenu_AddButton(info, level)
		end
		info.hasArrow = nil
	else -- In a party
		for i=1, MAX_PARTY_MEMBERS+1, 1 do
			local candidate = GetMasterLootCandidate(i)
			if candidate then
				randoms[#randoms + 1] = i
				if candidate == playerName then playerIndex = i end
				info.text = candidate
				info.value = i
				info.colorCode = menuColorCodes[CandidateUnitClass(candidate)]
				info.func = GroupLootDropDown_GiveLoot
				UIDropDownMenu_AddButton(info, level)
			end
		end
	end

	info.func = GroupLootDropDown_GiveLoot
	info.colorCode = HIGHLIGHT_FONT_COLOR_CODE

	if #randoms > 0 then
		local value = randoms[math.random(1, #randoms)]
		info.text = ("Joueur aléatoire (%s)"):format(GetMasterLootCandidate(value))
		info.value = value
		info.icon = "Interface\\Buttons\\UI-GroupLoot-Dice-Up"
		UIDropDownMenu_AddButton(info, level)
	end

	if playerIndex then
		info.text = "Ramasser soi-même"
		info.value = playerIndex
		info.icon = "Interface\\Buttons\\UI-GroupLoot-Coin-Up"
		UIDropDownMenu_AddButton(info, level)
	end
end

UIDropDownMenu_Initialize(GroupLootDropDown, GroupLootDropDown_Initialize, "MENU")
